package han.chensing.latos.common.level.content.timeLine.valuebook;

import java.io.Serial;
import java.io.Serializable;

import han.chensing.latos.common.level.content.timeLine.motion.interpolator.Interpolator;

public class ValueBookEntry implements Serializable {
    @Serial
    private static final long serialVersionUID = 2337291578298402511L;

    private float relativeFromTime,relativeToTime;
    private float fromValue,toValue;
    private float duration;
    private Interpolator interpolator;
    private float[] cachedValue;
    private boolean isCached;

    public float getValue(float relativeTime){
        float percent=relativeTime/duration;
        if (isCached){
            int index=Math.round(percent*(cachedValue.length-1));
            return cachedValue[index];
        }
        float delta=toValue-fromValue;
        return interpolator.value(percent) * delta+fromValue;
    }

    public float getRelativeFromTime() {
        return relativeFromTime;
    }

    public float getRelativeToTime() {
        return relativeToTime;
    }

    public float getFromValue() {
        return fromValue;
    }

    public float getToValue() {
        return toValue;
    }

    public float getDuration() {
        return duration;
    }

    public Interpolator getInterpolator() {
        return interpolator;
    }

    public float[] getCachedValue() {
        return cachedValue;
    }

    public boolean isCached() {
        return isCached;
    }

    public void setRelativeFromTime(float relativeFromTime) {
        this.relativeFromTime = relativeFromTime;
    }

    public void setRelativeToTime(float relativeToTime) {
        this.relativeToTime = relativeToTime;
    }

    public void setFromValue(float fromValue) {
        this.fromValue = fromValue;
    }

    public void setToValue(float toValue) {
        this.toValue = toValue;
    }

    public void setDuration(float duration) {
        this.duration = duration;
    }

    public void setInterpolator(Interpolator interpolator) {
        setCached(false);
        this.interpolator = interpolator;
    }

    public void setCachedValue(float[] cachedValue) {
        setCached(true);
        this.cachedValue = cachedValue;
    }

    public void setCached(boolean cached) {
        isCached = cached;
    }

    public void updateDuration(){
        setDuration(this.relativeToTime-this.relativeFromTime);
    }
}
